package com.web.service.impl;

import com.dto.BaseDto;
import com.dto.MessageEnum;
import com.entity.Condition;
import com.entity.Employrecord;
import com.exception.CustomException;
import com.page.PageInfo;
import com.web.dao.EmployrecordDao;
import com.web.service.CustomerServiceForEmployrecord;
import com.web.service.EmployrecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@Service
public class EmployrecordServiceImpl implements EmployrecordService {
    @Resource
    EmployrecordDao employrecordDao;
    @Resource
    CustomerServiceForEmployrecord customerServiceForEmployrecord;

    public BaseDto<Employrecord> getAll() throws Exception {
        List<Employrecord> employrecords = employrecordDao.getAll();
        MessageEnum messageEnum = employrecords.size() > 0? MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETALLSTUDENTNODATA;
        BaseDto<Employrecord> dto = new BaseDto(messageEnum,employrecords);

        return dto;
    }






    public BaseDto<Employrecord> getEmployrecordsByPage(PageInfo pageInfo) throws Exception {
        List<Employrecord> employrecords = employrecordDao.getEmployrecordsByPage(pageInfo);
        MessageEnum messageEnum = employrecords.size() > 0? MessageEnum.GETCUSTOMERBYPAGESUCCESS:MessageEnum.GETCUSTOMERBYPAGEFAIL;
        BaseDto<Employrecord> dto = new BaseDto(messageEnum,employrecords);

        return dto;
    }

    /**
     * 新增合同需要扣款成功才可以
     * @param employrecord
     * @return
     * @throws Exception
     */
    //rollbackFor  当发生什么异常的时候 回滚全部的sql语句
    @Transactional(value="transactionManager",propagation = Propagation.REQUIRED,rollbackFor={CustomException.class})
    public BaseDto<Employrecord> add(Employrecord employrecord) throws Exception {
        int result = employrecordDao.add(employrecord);//添加合同
        //根据合同提供的
        customerServiceForEmployrecord.subCustomerCost(employrecord.getCid(),employrecord.getCost());

        MessageEnum messageEnum = result > 0? MessageEnum.ADDEMPLOYRECORDSUCCESS:MessageEnum.ADDEMPLOYRECORDSFAIL;
        BaseDto<Employrecord> dto = new BaseDto(messageEnum,result);

        return dto;
    }

    /**
     * 修改合同金额
     * @param employrecord
     * @return
     * @throws Exception
     */
    public BaseDto<Employrecord> updateCost(Employrecord employrecord) throws Exception {
        int result = employrecordDao.update(employrecord);
        MessageEnum messageEnum = result > 0? MessageEnum.ADDCUSTOMERSUCCESS:MessageEnum.ADDCUSTOMERFAIL;
        BaseDto<Employrecord> dto = new BaseDto(messageEnum,result);

        return dto;
    }

    /**
     * 修改合同状态
     * @param employrecord
     * @return
     * @throws Exception
     */
    public BaseDto<Employrecord> updateState(Employrecord employrecord) throws Exception {
        int result = employrecordDao.add(employrecord);
        MessageEnum messageEnum = result > 0? MessageEnum.ADDCUSTOMERSUCCESS:MessageEnum.ADDCUSTOMERFAIL;
        BaseDto<Employrecord> dto = new BaseDto(messageEnum,result);

        return dto;
    }


    /**
     * 搜索分页查询
     * @param pageInfo
     * @return
     */
    public BaseDto  searchEmployeesByPage(PageInfo pageInfo, List<Condition> conditions) throws Exception {
        //枚举的具体值由三元表达式决定
        List<Employrecord> members = employrecordDao.searchEmployeesByPage(pageInfo,conditions);
        MessageEnum messageEnum = members.size()>0?MessageEnum.SEARCHEMPLOYBYPAGESUCCESS:MessageEnum.SEARCHEMPLOYBYPAGEFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.settList(members);//将查询结果也放入dto
        return dto;
    }
}
